package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    void insert(Orders orders);


    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    void update(Orders orders);


    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);


    @Select("select * from orders where id = #{id}")
    Orders getByid(Long id);

    @Select("select status,count(status) as count from orders where order_time between #{begin} and #{end} group by status;")
    List<Map<String,Object>> findall(LocalDate begin, LocalDate end);

    @Update("update orders set status = #{status} where id = #{id}")
    void updatezt(OrdersConfirmDTO ordersConfirmDTO);

    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);

    @Select("select sum(amount) from orders where order_time between #{beginTime} and #{endTime} and status = #{starts}")
    Double getTurnover(LocalDateTime beginTime, LocalDateTime endTime, int starts);

    @Select("select count(*) from orders where order_time between #{beginTime} and #{endTime}")
    Integer OrderCount(LocalDateTime beginTime, LocalDateTime endTime);

    @Select("select count(*) from orders where order_time between #{beginTime} and #{endTime} and status =6")
    Integer cancelCount(LocalDateTime beginTime, LocalDateTime endTime);

    @Select("SELECT name, sum(order_detail.number) AS total\n" +
            "FROM order_detail\n" +
            "LEFT JOIN sky_take_out.orders o ON order_detail.order_id = o.id\n" +
            "WHERE o.order_time BETWEEN #{begin} AND #{end}\n" +
            "GROUP BY name\n" +
            "ORDER BY total DESC\n" +
            "LIMIT 10;")
    List<Map<String, Object>> findtop10(LocalDateTime begin, LocalDateTime end);

    @Select("SELECT sum(amount)/count(*) from orders where status=5 and order_time between 'begin' and 'end';")
    Double getunitPrice(LocalDate begin, LocalDate end);

    @Select("select count(*) from orders where order_time between #{begin} and #{end}")
    Integer countbyday(LocalDate begin, LocalDate end);
}

